package com.xxzx.publicprevention.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xxzx.publicprevention.pojo.RecordDO;
import com.xxzx.publicprevention.pojo.RecordfilerelationDO;
import com.xxzx.publicprevention.pojo.ResultMap;
import com.xxzx.publicprevention.pojo.UserDO;
import com.xxzx.publicprevention.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@CrossOrigin
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/getAllUsers", method = RequestMethod.GET)
    @ResponseBody
    public ResultMap<List<UserDO>> getAllUsers(@RequestParam("page") Integer curr, @RequestParam("limit") Integer size) {
        List<UserDO> list = new ArrayList<>();
        ResultMap<List<UserDO>> resultMap = new ResultMap<>();
        try {
            int total = userService.getCount();
            list = userService.getUserByPage(curr - 1, size);
            resultMap = new ResultMap<>(0, "", total, list);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return resultMap;
    }

    @RequestMapping(value = "/editUser", method = RequestMethod.POST)
    @ResponseBody
    public Boolean editUser(String user) {
        boolean flag = false;
        try {
            UserDO userDO = JSON.parseObject(user, UserDO.class);
            userService.editUser(userDO);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @RequestMapping(value = "/getUser", method = RequestMethod.GET)
    @ResponseBody
    public UserDO getUser(Integer id) {
        UserDO user = userService.getUser(id);
        return user;
    }

//    //通过 openid查询用户 没有记录则新增
//    @RequestMapping(value = "/getUserByOpenId", method = RequestMethod.GET)
//    @ResponseBody
//    public UserDO getUserByOpenId(String openId) {
//        UserDO user = userService.getUserByOpenId(openId);
//        return user;
//    }

    //通过 openid查询用户 没有记录则新增（与之前方法不同，会新增wxName和unionId），重写方法不更改的原因是怕PC端会使用之前那个方法
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
    @ResponseBody
    public UserDO getUserInfo(String openId, String wxName, String unionId) {
        UserDO user = userService.getUserInfo(openId, wxName, unionId);
        return user;
    }

    //通过openId和wxName获取user信息,没有则新增, 若wx名有变化则更新微信名
    @RequestMapping(value = "/getUserByOpenIdAndwxName", method = RequestMethod.GET)
    @ResponseBody
    public UserDO getUserByOpenIdAndwxName(String openId, String wxName) {
        UserDO user = userService.getUserByOpenIdAndwxName(openId, wxName);
        return user;

    }

    //通过id查询用户有没有关联手机
    @RequestMapping(value = "/checkUserHasPhoneNumber", method = RequestMethod.GET)
    @ResponseBody
    public Boolean checkUserPhoneNumber(Integer userId) {
        return userService.checkUserPhoneNumber(userId);
    }

    @RequestMapping(value = "/bindPhoneNumberAndRealName", method = RequestMethod.POST, produces = {"application/json;charset=utf8"})
    @ResponseBody
    public String bindPhoneNumberAndRealName(@RequestBody HashMap<String, String> bind_data) {
        int userId = Integer.parseInt(bind_data.get("userId"));
        String phoneNumber = bind_data.get("phoneNumber");
        String realName = bind_data.get("realName");
        Map<String,String> result = userService.bindPhoneNumberAndRealName(userId,phoneNumber,realName);
        return JSONObject.toJSONString(result);
    }

    @RequestMapping(value = "/updateUserLocation", method = RequestMethod.POST, produces = {"application/json;charset=utf8"})
    @ResponseBody
    public String updateUserLocation(@RequestBody HashMap<String, String> locationData) {
        int userId = Integer.parseInt(locationData.get("userId"));
        String locationCity = locationData.get("locationCity");
        String locationStreet = locationData.get("locationStreet");
        Map<String,String> result = userService.updateUserLocation(userId,locationCity,locationStreet);
        return JSONObject.toJSONString(result);
    }

    @RequestMapping(value = "/getUserByPhoneNumber", method = RequestMethod.GET)
    @ResponseBody
    public String getUserByPhoneNumber(String phoneNumber) {
        return JSONArray.toJSONString(userService.getUserByPhoneNumber(phoneNumber));
    }

    @RequestMapping(value = "/getAllUserInOpenIdNotNull", method = RequestMethod.GET)
    @ResponseBody
    public String getAllUserInOpenIdNotNull() {
        return JSONArray.toJSONString(userService.getAllUserInOpenIdNotNull());
    }

    @RequestMapping(value = "/getUserByLocationStreet", method = RequestMethod.GET)
    @ResponseBody
    public String getUserByLocationStreet(String street) {
        return JSONArray.toJSONString(userService.getUserByLocationStreet(street));
    }

    @RequestMapping(value = "/getUserByLocationCity", method = RequestMethod.GET)
    @ResponseBody
    public String getUserByLocationCity(String city) {
        return JSONArray.toJSONString(userService.getUserByLocationCity(city));
    }
}

